package com.physics.mps.module.comsol.builder.common;

import com.physics.mps.module.comsol.builder.Builder;
import com.physics.mps.module.comsol.vo.CommonFeatureVo;
import com.physics.mps.module.comsol.vo.FunctionVo;
import org.springframework.util.CollectionUtils;

import java.util.*;

import static com.physics.mps.module.comsol.builder.CommonPool.ABBR_MAP;

/**
 * @author mcsoft
 * @date 2023/10/23 11:38
 * @description comsol java 全局定义脚本构建
 */
public class CommonBuilder {

    private final Builder builder;


    public CommonBuilder(Builder builder) {
        this.builder = builder;
    }


    /**
     * 构建全局降阶模型输入
     * @param commonFeatureVo
     */
    public void buildCommonFeature(CommonFeatureVo commonFeatureVo) {
        if (null == commonFeatureVo) {
            return;
        }
        String definitions = commonFeatureVo.getDefinitions();
        String commonFeatureTag = builder.uniqueTag("commonFeature");
        String definitionsTag = builder.uniqueTag(ABBR_MAP.get(definitions));

        builder.change(2, 2);
        builder.append("CommonFeature ");
        builder.append(commonFeatureTag);
        builder.append(" = model.common().create(");
        builder.value(definitionsTag);
        builder.append(", ");
        builder.value(definitions);
        builder.append(", \"\");");

        List<String> names = commonFeatureVo.getNames();
        List<String> expressions = commonFeatureVo.getExpressions();
        int len;
        if (CollectionUtils.isEmpty(names) || CollectionUtils.isEmpty(expressions) || (len = names.size()) != expressions.size()) {
            return;
        }

        for (int i = 0; i < len; i++) {
            String name = names.get(i);
            String expression = expressions.get(i);
            builder.change(2, 2);
            builder.append(commonFeatureTag);
            builder.append(".setIndex(\"name\",");
            builder.value(name);
            builder.append(", ");
            builder.append(i);
            builder.append(");");
            builder.change(1, 2);
            builder.append(commonFeatureTag);
            builder.append(".setIndex(\"expression\",");
            builder.value(expression);
            builder.append(", ");
            builder.append(i);
            builder.append(");");
        }

    }

    /**
     * 构建函数
     * @param functionVos
     */
    public void buildFunction(List<FunctionVo> functionVos) {
        if (CollectionUtils.isEmpty(functionVos)) {
            return;
        }
        builder.change(2, 0);
        for (FunctionVo functionVo : functionVos) {
            buildFunction(functionVo);
        }
    }


    /**
     * 构建函数
     * @param functionVo
     */
    public void buildFunction(FunctionVo functionVo) {

        String func = functionVo.getFunc();
        String tableType = functionVo.getTableType();
        String funcTag = builder.uniqueTag(ABBR_MAP.get(func));
        String functionFeature = builder.uniqueTag("functionFeature");

        builder.change(2, 2);
        builder.append("FunctionFeature ");
        builder.append(functionFeature);
        builder.append(" = model.func().create(");
        builder.value(funcTag);
        builder.append(", ");
        builder.value(func);
        builder.append(");");

        List<List<String>> table = functionVo.getTable();
        if (CollectionUtils.isEmpty(table)) {
            return;
        }
        for (int i = 0; i < table.size(); i++) {
            builder.change(1, 0);
            List<String> params = table.get(i);
            for (int j = 0; j < params.size(); j++) {
                builder.change(1, 2);
                builder.append(functionFeature);
                builder.append(".setIndex(");
                builder.value(tableType);
                builder.append(", ");
                builder.value(params.get(j));
                builder.append(", ");
                builder.append(i);
                builder.append(", ");
                builder.append(j);
                builder.append(");");
            }
        }

        builder.change(1, 2);
        builder.append(functionFeature);
        builder.append(".set(\"argunit\", \"Hz\");");

        builder.change(1, 2);
        builder.append(functionFeature);
        builder.append(".set(\"fununit\", \"m^2/s^3\");");

    }
}
